from pathlib import Path
from datetime import datetime
import uuid
from jinja2 import Template
from app.utils.logger import logger

class ReportService:
    def __init__(self):
        self.reports_dir = Path("reports")
        self.reports_dir.mkdir(exist_ok=True)
    
    async def generate_report(self, analysis_result: str, filename: str, prompt: str) -> Path:
        """生成HTML报告"""
        try:
            # HTML模板
            template_str = """
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频分析报告</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .header h1 {
            margin: 0;
            font-size: 2.5em;
            font-weight: 300;
        }
        .content {
            padding: 30px;
        }
        .info-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #4facfe;
        }
        .info-section h3 {
            margin-top: 0;
            color: #333;
        }
        .result-section {
            background: #fff;
            padding: 25px;
            border-radius: 8px;
            border: 1px solid #e9ecef;
            margin-bottom: 20px;
        }
        .result-content {
            white-space: pre-wrap;
            line-height: 1.8;
            color: #444;
            font-size: 16px;
        }
        .footer {
            text-align: center;
            padding: 20px;
            color: #666;
            border-top: 1px solid #e9ecef;
        }
        .timestamp {
            color: #888;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🎬 视频分析报告</h1>
            <p>基于InternVL3.5的智能视频理解</p>
        </div>
        
        <div class="content">
            <div class="info-section">
                <h3>📋 分析信息</h3>
                <p><strong>文件名称：</strong>{{ filename }}</p>
                <p><strong>分析提示：</strong>{{ prompt }}</p>
                <p><strong>生成时间：</strong>{{ timestamp }}</p>
            </div>
            
            <div class="result-section">
                <h3>🔍 分析结果</h3>
                <div class="result-content">{{ analysis_result }}</div>
            </div>
        </div>
        
        <div class="footer">
            <p class="timestamp">报告生成于 {{ timestamp }}</p>
            <p>Powered by InternVL3.5 | 视频理解分析工具 M0</p>
        </div>
    </div>
</body>
</html>
            """
            
            # 渲染模板
            template = Template(template_str)
            html_content = template.render(
                filename=filename,
                prompt=prompt,
                analysis_result=analysis_result,
                timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            )
            
            # 生成报告文件
            report_id = str(uuid.uuid4())
            report_path = self.reports_dir / f"report_{report_id}.html"
            
            with open(report_path, 'w', encoding='utf-8') as f:
                f.write(html_content)
            
            logger.info(f"报告已生成: {report_path}")
            return report_path
            
        except Exception as e:
            logger.error(f"报告生成失败: {str(e)}")
            raise Exception(f"报告生成失败: {str(e)}")